package taiyuaneltyl.com.my_mvp.Utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import taiyuaneltyl.com.my_mvp.R;


/**
 *    执行加载操作时候显示的dialog，单例模式
 * Created by Administrator on 2017/4/17.
 */

public class  LoadingDialog {

    private Context context;
    private Dialog dialog;
    private TextView tvDialogMsg;
    private static LoadingDialog loadingDialog;

    public LoadingDialog(Context context) {
        this.context = context;
        if (dialog==null){
            View view = LayoutInflater.from(context).inflate(R.layout.dialog, null);
            dialog = new Dialog(context, R.style.alert_dialog);
            dialog.setContentView(view);
            tvDialogMsg = ((TextView)view.findViewById(R.id.tvDialogMsg));
        }
    }

    public static LoadingDialog getInstance(Context context){
        if (loadingDialog==null){
            loadingDialog = new LoadingDialog(context.getApplicationContext());
        }
        return loadingDialog;
    }

    public void setLayoutParams(int gravity){
        dialog.getWindow().setGravity(gravity);
    }

    //显示默认为居中
    public void show(String msg) {
        if (this!=null||msg!=null) {
            tvDialogMsg.setText(msg);
            dialog.show();
        }
    }

    //自定义显示位置
    public void show(String msg, int gravity) {
        setLayoutParams(gravity);
        tvDialogMsg.setText(msg);
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
